<?
require_once("database.php");

	class message_handler extends database{
		
		public $result;
		
		function __construct(){
			parent::__construct();
		}

                function  __destruct() {

                }

                function compose_message($arr_user, $subject, $msg_body){
                    //compose message
                    //pertama bikin dulu grup baru yang anggotanya adalah semua element $arr_user
                    //nama grup = Group 1, 2, 3, dst.
                    $q = "SELECT max(group_id) AS maks FROM group_list";
                    $this->exec_q($q, "maks");
                    $id = $this->result[0];
                    $id++;
                    $g_name = "Group ". $id;

                    //buat group
                    $q_group = "INSERT INTO group_list (group_name) VALUES ('$g_name')";
                    $this->connect();
                    mysql_query($q_group, $this->conn);
                    mysql_close();

                    //masukin anggotanya
                    for ($i=0; $i<count($arr_user); $i++){
                        $user = $arr_user[$i];

                        $q_reg = "INSERT INTO group_members VALUES ($id,'$user')";
                        $this->connect();
                        mysql_query($q_reg, $this->conn);
                    }
                    mysql_close();

                    //bikin message baru di tabel message
                    $q_msg = "INSERT INTO message (group_id, subject, msg_body) VALUES ($id,'$subject','$msg_body')";
                    $this->connect();
                    mysql_query($q_msg, $this->conn);
                    mysql_close();
                    
                }

                function reply_message($group_id, $msg_id, $sender, $body){
                    $q = "INSERT INTO reply_message(group_id,msg_id,sender,body) VALUES ($group_id,$msg_id,'$sender','$body')";
                    $this->connect();
                    mysql_query($q, $this->conn);
                    mysql_close;
                }

                function view_replies($group_id, $msg_id){
                    $q = "SELECT * FROM reply_message WHERE group_id=$group_id AND msg_id=$msg_id ORDER BY reply_id";
                    $this->exec_q($q,"sender");
                    $res[0] = $this->result;
                    $this->exec_q($q,"body");
                    $res[1] = $this->result;

                    $this->result = $res;
                }

		
		function retr_message_col($usr_id){
			$query = "SELECT g_id,message.subject,message.msg_body,message.msg_id FROM
                            (SELECT group_members.group_id AS g_id FROM group_list,group_members WHERE group_members.group_id=group_list.group_id AND usr_member='$usr_id')
                            AS hasil,message WHERE g_id=message.group_id";
                        $query_2 = "SELECT DISTINCT g_id FROM
                            (SELECT group_members.group_id AS g_id FROM group_list,group_members WHERE group_members.group_id=group_list.group_id AND usr_member='$usr_id')
                            AS hasil,message WHERE g_id=message.group_id";



			$this->exec_q($query,"subject");
                        if ($this->result == NULL){
                            $res[0] = NULL;
                        }
                        $res[0] = $this->result;
                        //echo $res[0][0];

                        $this->exec_q($query,"msg_body");
			if ($this->result == NULL){
                            $res[1] = NULL;
                        }
                        $res[1] = $this->result;

                        //list group_id
                        $this->exec_q($query_2,"g_id");
                        if ($this->result == NULL){
                            $res[2] = NULL;
                        }
			$res[2] = $this->result;


                         $this->exec_q($query,"msg_id");
                        if ($this->result == NULL){
                            $res[4] = NULL;
                        }
			$res[4] = $this->result;


                        if ($this->result == NULL){
                            $mem = NULL;
                        }
                        else{
                            //list anggota grupnya siapa aja
                            for($i=0; $i<count($res[2]); $i++){
                                $g_id = $res[2][$i];
                                $q_mbr = "SELECT usr_member FROM group_members WHERE group_id=$g_id";
                                $this->exec_q($q_mbr,"usr_member");

                                for ($j=0; $j<count($this->result); $j++){
                                    $mem .= $this->result[$j]. ";";
                                }
                                $mem .= "/";
                            }
                        }

                        $res[3] = $mem;

                        if ($this->result == NULL){
                            $res = NULL;
                        }

                        $this->result = $res;
		}

                function parse_group_members($members){
                    $one_group = explode('/',$members);

                    
                    for ($i=0; $i<count($one_group); $i++){
                        $str = $one_group[$i];
                        $arr_members[$i] = explode(';', $str);
                    }
                    //echo $arr_members[0][0];
                    return $arr_members;
                }
	
		function retr_messages($usr_id){
			$res_b = $this->retr_message_col($usr_id,"msg_body");
			$res_s = $this->retr_message_col($usr_id,"subject");
			
                        $this->result[0] = $res_b;
                        $this->result[1] = $res_s;
		}

                function retr_msgID_byGroup($group_id){
                    $q = "SELECT msg_id FROM message WHERE group_id='$group_id'";

                    $this->exec_q($q, "msg_id");
                    //result -> array of ID
                }
		
		function filter_read($usr_id){
			$query = "SELECT msg_read FROM user WHERE usr_email='$usr_id'";
			$res = $this->exec_q($query,"msg_read");
			
			foreach ($res AS $r){
				$e = explode(",",$r);
			}
                        
                        $this->result = $e; //array
		}

                function filter_inbox($usr_id){
			$query = "SELECT msg_inbox FROM user WHERE usr_email='$usr_id'";
			$res = $this->exec_q($query,"msg_inbox");

			foreach ($res AS $r){
				$e = explode(",",$r);
			}

                        $this->result = $e; //array
		}
	}
	
	
?>